﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WXApi.Model;
using WxTenpay.Service;
using WxTenpay.Service.Model;

namespace WXApi.Controllers.SysApi
{

    /// <summary>
    /// 获取菜单信息类
    /// </summary>
    [Route("[controller]/[action]")]
    [ApiController]
    [Action]
    public class SysAdminController : ReqDataControllerBase
    {
        private readonly ISysMenu sysmenu;
        private readonly ISysUser sysuser;
        private readonly ISysRoleAuthorization isysroleauthorization;
        private readonly ISysMenuButton sysmenubutton;
        private readonly ISysLog syslog;

        public SysAdminController(ISysLog _syslog,ISysMenu _sysmenu, ISysUser _sysuser, ISysRoleAuthorization _isysroleauthorization, ISysMenuButton _sysmenubutton)
        {
            syslog = _syslog;
            sysmenu = _sysmenu;
            sysuser = _sysuser;
            isysroleauthorization = _isysroleauthorization;
            sysmenubutton = _sysmenubutton;
        }




        #region 获取菜单数据
        /// <summary>
        /// 获取菜单数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetMenu()
        {
            try
            {
                var token = HttpContext.Items["token"].ToString();
                var usergid = GetUser(token);



                //获取用户信息
                var usermodel = sysuser.GetModel(x => x.GuId == usergid);
                if (usermodel == null)
                    return Error("参数异常！");



                if (usermodel.UserName == "admin") //管理员显示全部信息
                {
                    var btnlis = sysmenubutton.GetList();
                    var list = sysmenu.GetList();
                    var data = getAdministratormenu(list, btnlis);
                    return Success(data);
                }
                else  //其他用户权限信息
                {

                    List<string> meuns = new List<string>();
                    List<string> btns = new List<string>();

                    //获取用户权限信息 、带角色权限
                    var isysroleauthorizations = isysroleauthorization.GetList(x => x.UserGid == usermodel.GuId || x.UserGid == usermodel.RoleGuId);

                    isysroleauthorizations.ForEach(x =>
                    {
                        var meunmodel = meuns.FirstOrDefault(p => p == x.MenuGid);
                        if (meunmodel == null)
                        {
                            meuns.Add(x.MenuGid);
                        }
                        var Buttons = x.ButtonLis.Split(',');
                        btns= btns.Union(Buttons).ToList();

                    });

                   
                    string _meuns = string.Join(',', meuns);
                    string _btns = string.Join(',', btns);

                    var btnlis = sysmenubutton.GetList(x => _btns.Contains(x.GuId));
                    var list = sysmenu.GetList(x => _meuns.Contains(x.GuId));
                    var data = getAdministratormenu(list, btnlis);
                    return Success(data);

                }

            }
            catch (Exception ex)
            {
                ErrorLogDto model = new ErrorLogDto();
                model.data = "系统错误！";
                model._syslog = syslog;
                model.logmodel = new SysLogEntity { Action = "SysAdmin/GetMenu", Content = ex.ToString(), Name = "获取菜单数据" };
                return ErrorLog(model);
            }
        }
        #endregion


        /// <summary>
        /// 获取菜单列表数据
        /// </summary>
        /// <param name="lis"></param>
        /// <param name="ParentId"></param>
        /// <returns></returns>
        private object getAdministratormenu(List<SysMenuEntity> lis, List<SysMenuButtonEntity> btnlis, string ParentId = "0")
        {
            try
            {
                var list = lis.Where(x => x.ParentId == ParentId).Select(x =>
                  {
                      return new
                      {
                          menuid = x.Id,
                          icon = x.Icon,
                          menuname = x.Name,
                          type = x.Type,
                          url = x.Address,
                          btnlis = btnlis.Where(p => p.MenuGuId == x.GuId).Select(p => { return new { p.GuId, p.Name, p.Number }; }),
                          menus = getAdministratormenu(lis, btnlis, x.Id.ToString())
                      };


                  });

                return list;

            }
            catch (Exception)
            {

                throw;
            }

        }



        /// <summary>
        /// 获取其他用户菜单列表数据
        /// </summary>
        /// <param name="lis"></param>
        /// <param name="ParentId"></param>
        /// <returns></returns>
        private object getmenu(List<SysMenuEntity> lis, List<SysMenuButtonEntity> btnlis, string ParentId = "0")
        {
            try
            {
                var list = lis.Where(x => x.ParentId == ParentId).Select(x =>
                {
                    return new
                    {
                        menuid = x.Id,
                        icon = x.Icon,
                        menuname = x.Name,
                        type = x.Type,
                        url = x.Address,
                        btnlis = btnlis.Where(p => p.MenuGuId == x.GuId).Select(p => { return new { p.GuId }; }),
                        menus = getmenu(lis, btnlis, x.Id.ToString())
                    };


                });

                return list;

            }
            catch (Exception)
            {

                throw;
            }

        }



    }
}
